package org.example.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import java.util.Date;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.example.entity.Updateable;
import org.springframework.stereotype.Component;

/**
 * @Title : aa
 * @Description: :
 * @author: libo@juzishuke.com
 * @date: 2023/11/16 16:21
 * @Version:1.0
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    // 插入式的填充策略
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("开始执行了插入操作---------------------------------------------------------------");
        if(metaObject.getOriginalObject() instanceof Updateable){
            Updateable obj = (Updateable)metaObject.getOriginalObject();
            obj.setUpdateTime(new Date());
            obj.setCreateTime(new Date());
        }
    }

    // 更新式的填充策略
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("开始执行了更新操作---------------------------------------------------------------");
        if(metaObject.getOriginalObject() instanceof Updateable){
            Updateable obj = (Updateable)metaObject.getOriginalObject();
            obj.setUpdateTime(new Date());
        }
    }
}
